package com.me.config;

import freemarker.template.TemplateModelException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * Created by Administrator on 2018/11/30.
 */
@Configuration
public class FreemarkerConfig  implements ServletContextListener {
    @Autowired
    private freemarker.template.Configuration conf;
    @Override
    public void contextDestroyed(ServletContextEvent arg0) {

    }
    private static final Logger logger = LoggerFactory.getLogger(FreemarkerConfig.class);
    @PostConstruct
    public void setSharedVariable() throws TemplateModelException {
        //configuration.setSharedVariable("user_topics_tag", userTopicDirective);
//        System.out.println("==========================================      "+siteurl);
//        conf.setSharedVariable("siteurl","");



    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        sce.getServletContext().setAttribute("lang", "zh");

        String webRoot = sce.getServletContext().getContextPath();
        logger.info("加载 servlet...");
        sce.getServletContext().setAttribute("webRoot", webRoot);//Web根目录
        logger.info("Web根目录:"+sce.getServletContext().getAttribute("webRoot"));
        sce.getServletContext().setAttribute("resRoot", webRoot+"/statics");//资态资源根目录
        logger.info("资态资源根目录:"+sce.getServletContext().getAttribute("resRoot"));
        try {
            conf.setSharedVariable("siteurl",webRoot);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
